package cn.com.oceansoft.osc.ms.schedule;

import cn.com.oceansoft.osc.ms.config.CallPoliceConfig;
import cn.com.oceansoft.osc.ms.persistence.ICallPoliceMapper;
import cn.com.oceansoft.osc.ms.schedule.entity.*;
import cn.com.oceansoft.osc.ms.schedule.service.*;
import cn.com.oceansoft.osc.ms.schedule.service.CallPoliceService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author wuzy
 * @created 2017-03-14-10:32
 * @email wzy@oceansoft.com.cn
 */
//@Component
public class CallPoliceSchedule {

    @Resource
    ICallPoliceMapper callpoliceMapper;
    @Resource
    CallPoliceService callPoliceService;

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void executeCallPoliceTask() throws Exception {
        List<CallPoliceAlarm> listcall = callpoliceMapper.GetCallPolice();
        System.out.println("数据库报警条数有：" + listcall.size());
        for (CallPoliceAlarm call : listcall) {
            CallPolice callPolice = new CallPolice();
            callPolice.setGuid(call.getGUID());
            callPolice.setAddress(call.getADDRESS());
            callPolice.setCrContent(call.getCRCONTENT());
            callPolice.setIsPhone(call.getISPHONE());
            callPolice.setUserCode(call.getUSERCODE());
            callPolice.setUserName(call.getUSERNAME());
            callPolice.setUserPhone(call.getUSERPHONE());
            callPolice.setUserAddress(call.getUSERADDRESS());
            callPolice.setUnitcontactor(call.getUNITCONTACTOR());
            callPolice.setHandPhone(call.getHANDPHONE());
            callPolice.setToorGid(call.getTOORGID());
            callPolice.setOpenid(call.getOPENID());
            callPolice.setLatitude(call.getEARTH_LAT());
            callPolice.setLongitude(call.getEARTH_LONG());
            //callPolice.setToorGid("530100000000");
            callPolice.setToorgName(call.getTOORGNAME());
            System.out.println("getToorGid：" + callPolice.getToorGid());
            if(callPolice.getToorGid().equals(""))
            {
                callPolice.setToorGid("530100000000");
            }
            CallPoliceComno comno = CallPoliceComno.getInstance(callPolice.getToorGid());
            String n = comno.getComNO();
            callPolice.setComno(n);
            callPolice.setStatus(call.getSTATE());
            callPolice.setTemp1(call.getREMARK1());
            callPolice.setTemp2(call.getREMARK2());
            callPolice.setTemp3(call.getREMARK3());
            List<CallPoliceAttach> listattach = new ArrayList<CallPoliceAttach>();
            List<CallPoliceMatil> listattaches = callpoliceMapper.GetCallPoliceMatil(call.getGUID());
            System.out.println("数据库报警附件数据的条数有：" + listattaches.size());
            for (CallPoliceMatil Matil : listattaches) {
                CallPoliceAttach calpolice = new CallPoliceAttach();
                calpolice.setFguid(Matil.getGUID());
                calpolice.setPoliceRepGuid(call.getGUID());
                try {
                    calpolice.setFname(Matil.getURL().substring( Matil.getURL().lastIndexOf("/")+1));
                }
                catch (Exception ex)
                {
                    calpolice.setFname(Matil.getGUID()+".jpg");
                }
                //calpolice.setFname(Matil.getNAME());
                calpolice.setAttachType("0");
                calpolice.setAttachPath("http://10.166.113.228/" + Matil.getURL());
                listattach.add(calpolice);
            }
            boolean bol = callPoliceService.policeReport2DS(callPolice, listattach,call.getGUID());
            //boolean bol=true;
            if (bol) {
                //修改这条报警数据上传的状态
                int i = callpoliceMapper.updateAlarmState(call.getGUID());
                //记录错误日志
                callPoliceService.CreateLog("","","1","推送到指挥中心成功","",call.getGUID(),"alarm");
            } else {
                //记录错误日志
                callPoliceService.CreateLog("","","0","推送到指挥中心失败","",call.getGUID(),"alarm");
            }
        }
        return;
    }

    //轮询推送会话信息给DS
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void SendCallPoliceTask() throws Exception {
        List<CallPoliceChat> listcall = callpoliceMapper.GetChat();
        System.out.println("数据库报警会话条数有：" + listcall.size());
        for (CallPoliceChat call : listcall) {
            CallPoliceChatDS callPolice = new CallPoliceChatDS();
            callPolice.setGUID(call.getALARMGUID());
            callPolice.setPHONE(call.getPHONE());
            callPolice.setOPENID(call.getOPENID());
            callPolice.setCHANGETYPE(call.getCHANGETYPE());
            callPolice.setCHANGECONTENT(call.getCHANGECONTENT());
            callPolice.setCHANGEDATE(call.getCHANGEDATE());
            callPolice.setLONG(call.getLONGITUDE());
            callPolice.setLAT(call.getLATITUDE());
            callPolice.setREMARK1(call.getREMARK1());
            callPolice.setREMARK2(call.getREMARK2());
            boolean bol = callPoliceService.policeReportChatDS(callPolice,call.getGUID());
            //boolean bol=true;
            if (bol) {
                //修改这条报警数据上传的状态
                int i = callpoliceMapper.updateChatStatus(call.getGUID());
                //记录错误日志
                callPoliceService.CreateLog("","","1","推送到指挥中心成功","",call.getGUID(),"chat");
            } else {
                //记录错误日志
                callPoliceService.CreateLog("","","0","推送到指挥中心失败","",call.getGUID(),"chat");
            }
        }
        return;
    }
}
